/**
Nastavitve

@author Gašper Tomažič
*/

package splash.core;


public class Settings
{
	public enum State {START, STOP, PAUSE, N, LOCK, STARTUP, SHUTDOWN};
	
	private State state;
	private int maxClients;
	private int port;
	private String password;
	private boolean debug;
        private int timeout;
        private OutControl stateCtrl;
        private boolean headers;
        /* 
        * select specific non-zero port, or 0 to select first free port
        * you can avoid PortBoud (in use) problems using 0
        */
	private int secondaryPort;
        
	public Settings(int maxC, int port, int sec, String password, boolean dbg, OutControl oc, boolean verb)
	{
		state = State.STOP;
		if(port >= 65536 || port < 1)
			throw new IllegalArgumentException("Neveljavna številka vrat.");

                if(sec >= 65536 || sec < 0)
			throw new IllegalArgumentException("Neveljavna številka vrat.");
                
		if(maxC < 1)
			throw new IllegalArgumentException("Premajhno št. možnih povezav");
		
		if(password == null)
			password = new String("");
		
		this.port = port;
                this.secondaryPort = sec;
		this.maxClients = maxC;
		this.password = Util.sha(password);
		this.debug = dbg;
                this.stateCtrl = oc;
                this.headers = verb;
            
                /* built-in settings*/
                this.timeout = 20;
	}
	
	public State getState()
	{
		return this.state;
	}
	
	public int getPort()
	{
		return this.port;
	}
        
        public int getSecondaryPort()
        {
            return (this.secondaryPort == 0 || Util.isBound(this.secondaryPort)) ? 0 : this.secondaryPort;
        }
	
	public int getMaxC()
	{
		return this.maxClients;
	}
	
	public String getPassword()
	{
		return this.password;
	}
	
	public boolean debugMode()
	{
		return this.debug;
	}
        
        public boolean getHeaders()
	{
		return this.headers;
	}

        public int getTimeout()
        {
            return this.timeout;
        }
	
	public void setPort(int port)
	{
                if(port >= 65536 || port < 1)
			throw new IllegalArgumentException("Neveljavna številka vrat.");
		this.port = port;
	}
        
        public void setSecondaryPort(int port)
	{
                if(port >= 65536 || port < 0)
			throw new IllegalArgumentException("Neveljavna številka vrat.");
		this.secondaryPort = port;
	}
	
	public void setMaxClients(int maxC)
	{
                if(maxC < 1)
                        throw new IllegalArgumentException("Premajhno stevilo dovoljenih povezav.");
		this.maxClients = maxC;
	}
	
	public void setPassword(String pwd)
	{
		this.password = Util.sha(pwd);
	}
	
	public void setState(State s)
	{
                /* Update state for state controler (GUI) */
                if(this.stateCtrl != null)
                    this.stateCtrl.setState(s);
                
		this.state = s;
	}
}